﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Site.Models;

namespace Site.Controllers
{
    public class MessageController : Controller
    {

        private IMessageRepository repository;
        public MessageController()
            : this(new MessageRepository())
        { }

        public MessageController(IMessageRepository rep)
        {
            repository = rep;
        }

        //
        // GET: /Message/


        public ActionResult Index()
        {
            return View(repository.GetReceivedMessages());
        }
        public ActionResult MessagesPartial(string type)
        {

            if (type == "received")
            {
                return PartialView("MessagesPartial", repository.GetReceivedMessages());
            }
            else
                return PartialView("SentMessagesPartial", repository.GetSentMessages());

        }

        //
        // GET: /Message/Details/5

        public ActionResult Details(Guid id)
        {

            return View(repository.GetMessageById(id));
        }

        //
        // GET: /Message/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Message/Create

        [HttpPost]
        public ActionResult SendMessage(Message model, string receiver = "")
        {
            // model.Receiver = receiver;
            var message = new Message();
            message.Sender = HttpContext.User.Identity.Name;
            message.Subject = model.Subject;
            message.Receiver = model.Receiver;
            message.Body = model.Body;
            message.MessageId = Guid.NewGuid();
            message.SentDate = DateTime.Now;
            message.Read = false;
            if (repository.IsMember(model.Receiver))
            {
                message.Receiver = model.Receiver;
            }
            try
            {
                TryUpdateModel(message);
                repository.NewMessage(message);
                return View("MessageSent");
            }
            catch
            {
                return View();
            }
        }
        public ActionResult SendMessage(string receiver)
        {
            return View();
        }


        //
        // GET: /Message/Edit/5
        public ActionResult ReadMessage(Guid id)
        {
            repository.ReadMessage(id);

            return View(repository.GetMessageById(id));
        }


        public ActionResult Delete(Guid id)
        {
            return View();
        }

        //
        // POST: /Message/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, Message message)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
